package com.fr.report.script.function;

import com.fr.report.script.Function;
import com.fr.report.script.Function.Type;
import com.fr.report.script.NormalFunction;
import com.fr.report.script.Primitive;
import com.fr.report.script.core.FArray;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class RANK extends NormalFunction
{
  public Object run(Object[] paramArrayOfObject)
  {
    if (paramArrayOfObject.length < 3)
      return Primitive.ERROR_NAME;
    double d1 = 0.0D;
    LinkedList localLinkedList = new LinkedList();
    int i = 0;
    for (int j = 0; j < paramArrayOfObject.length; ++j)
    {
      Object localObject = paramArrayOfObject[j];
      double d2 = 0.0D;
      if (localObject instanceof Number)
      {
        d2 = ((Number)localObject).doubleValue();
      }
      else if (localObject instanceof Boolean)
      {
        if (((Boolean)localObject).booleanValue())
          d2 = 1.0D;
      }
      else
      {
        if ((j != 0) && (j != paramArrayOfObject.length - 1) && (localObject instanceof FArray))
          break label117:
        localObject = null;
      }
      if (j == 0)
      {
        if (localObject == null)
          return Primitive.ERROR_NAME;
        d1 = d2;
      }
      else if (j == paramArrayOfObject.length - 1)
      {
        if (d2 != 0.0D)
          i = 1;
      }
      else
      {
        if (localObject instanceof FArray)
        {
          int l = 0;
          while (true)
          {
            if (l >= ((FArray)localObject).length())
              break label229;
            localLinkedList.add(((FArray)localObject).elementAt(l));
            ++l;
          }
        }
        if (localObject != null)
          localLinkedList.add(new Double(d2));
      }
    }
    label117: j = localLinkedList.size();
    label229: double[] arrayOfDouble = new double[j];
    int k = 0;
    Iterator localIterator = localLinkedList.iterator();
    while (localIterator.hasNext())
    {
      arrayOfDouble[k] = ((Number)localIterator.next()).doubleValue();
      ++k;
    }
    Arrays.sort(arrayOfDouble);
    if (i != 0)
    {
      for (k = 1; k < j + 1; ++k)
        if (d1 == arrayOfDouble[(k - 1)])
          break;
      if (k == j + 1)
        k = 0;
    }
    else
    {
      for (k = j; k > 0; --k)
        if (d1 == arrayOfDouble[(k - 1)])
        {
          k = j - k + 1;
          break;
        }
    }
    return new Integer(k);
  }

  public Function.Type getType()
  {
    return Function.OTHER;
  }

  public String getCN()
  {
    return "RANK(number,ref,order): 返回一个数在一个数组中的秩。(如果把这个数组排序，该数的秩即为它在数组中的序号。)\nNumber 所求秩的数。(可以是Boolean型，true=1，false=0)\nRef    可以是数组，引用，或一系列数，非实数的值被忽略处理(接受Boolean型，true=1，false=0)。\nOrder  指定求秩的参数，非零为升序，零为降序\n\n备注\n1.  RANK对重复的数返回相同的秩，但重复的数影响后面的数的秩，比如，在一组升序排列的整数中，如果5出现了2次，并且秩为3，那么6的秩为5 (没有数的秩是4).\n\n示例\nA1:A5 = 6, 4.5, 4.5, 2, 4\nRANK(A1,A1:A5,1) 即 6 的秩为 5.\n\nRANK(3,1,2,\"go\",3,4,1) = 3, \"go\"被忽略。";
  }

  public String getEN()
  {
    return "RANK(number,ref,order): Returns the rank of a number in a list of numbers. The rank of a number is its size relative to other values in a list. (If you were to sort the list, the rank of the number would be its position.)\nNumber    is the number whose rank you want to find. (Boolean is allowed, true will be treated as 1, false will be treated as 0).\nRef    is an array of, or a reference to, a list of numbers. Nonnumeric values in ref are ignored. (Boolean is allowed in ref, true will be treated as 1, false will be treated as 0).\nOrder    is a number specifying how to rank number.\n\nRemarks\n    RANK gives duplicate numbers the same rank. However, the presence of duplicate numbers affects the ranks of subsequent numbers. For example, in a list of integers sorted in ascending order, if the number 5 appears twice and has a rank of 3, then 6 would have a rank of 5 (no number would have a rank of 4).\n\nExample\nA1:A5 = 6, 4.5, 4.5, 2, 4\nRANK(A1,A1:A5,1)  Rank of 6 is 5.\n\n\nRANK(3,1,2,\"go\",3,4,1) = 3, \"go\" is ignored.";
  }
}